import 'package:flutter/material.dart';
import 'package:xws/utils/data_util.dart';
import 'package:xws/utils/log_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_text_field.dart';

/// 设置原价
class SetPrice extends StatelessWidget {
  const SetPrice({Key? key, this.init, this.onChanged}) : super(key: key);

  final double? init;
  final Function(double?)? onChanged;

  /// 值变化
  void _onChanged(String s) {
    try {
      if (s.isEmpty) {
        onChanged?.call(null);
        return;
      }
      final double? p = double.tryParse(s);
      onChanged?.call(p);
    } catch (e) {
      error(e);
      onChanged?.call(null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 140,
      child: AppTextField(
        height: 40,
        hintText: '入手价(选填)',
        textAlign: TextAlign.center,
        initialValue: init?.toString() ?? '',
        onChanged: _onChanged,
        fillColor: canvas(context),
        border: OutlineInputBorder(
          borderSide: BorderSide.none,
          borderRadius: BorderRadius.circular(4),
        ),
        contentPadding: const EdgeInsets.all(12),
        maxLength: 9,
        inputFormatters: onlyNum,
        keyboardType: TextInputType.number,
      ),
    );
  }
}
